package com.xcdh.commons.utils;

import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpUtils {
    public static void main(String[] args) {
        // Create an HTTP client
        // Create a proxy host
        HttpHost proxy = new HttpHost("s1.caiguoguo.org", 63305);
       // { name: '🇺🇸 美国 S1', type: ss, server: s1.caiguoguo.org, port: 63305, cipher: chacha20-ietf-poly1305, password: 92258c60-1696-4e47-914d-284eb5e60a70, udp: true }
        // 创建凭据提供程序，并设置代理服务器的账号和密码
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(
                new AuthScope(proxy),
                new UsernamePasswordCredentials("chacha20-ietf-poly1305", "92258c60-1696-4e47-914d-284eb5e60a70"));

        // Create request configuration with proxy
        RequestConfig config = RequestConfig.custom()
                .setConnectTimeout(3000)
            //     .setProxy(proxy)
                .build();

        CloseableHttpClient httpClient = HttpClients.custom()
                .setDefaultCredentialsProvider(credentialsProvider)
                .setDefaultRequestConfig(config)
                .build();

        try {
            // Create a new GET request
            URIBuilder builder = new URIBuilder("https://vuepress.vuejs.org/guide/#features");
            HttpGet request = new HttpGet(builder.build());
            request.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.51");
            // Send the request
            org.apache.http.HttpResponse response = httpClient.execute(request);

            // Get the response body
            String responseBody = EntityUtils.toString(response.getEntity());

            // Print the response body
            System.out.println(responseBody);
        } catch (Exception e) {
           e.printStackTrace();
        }
    }
}
